/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hanuma;

import java.util.*;
import java.io.*;
import com.google.gson.*;

/**
 *
 * @author niyaz
 */
public class FileStoredPersonReader {

    BufferedReader reader;
    Gson gson = new Gson();

    public FileStoredPersonReader(String file) throws Exception {
        try {
            reader = new BufferedReader(new FileReader(new File(file)),100000);
        } catch (Exception e) {
            throw new Exception("ca'nt fine file");
        }
    }

    class PersonIterator implements java.util.Iterator{

        String last;

        public PersonIterator() throws Exception {
            last = reader.readLine();
        }

        public void remove() {
        }

        public Person next() {
            if (last == null) {
                return null;
            }
           Person s = new Person();
           s.asString = last;
            Person p2 = (Person) gson.fromJson(last, s.getClass());
            try {
                last = reader.readLine();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return p2;
        }

        public boolean hasNext(){
            return (last != null);
        }
    }

    public Iterator getIterator(){
        try {
        return new PersonIterator();
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
